package boot.spring.service.impl;

import boot.spring.po.MessageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import boot.spring.po.Mail;
import boot.spring.service.Publisher;

import java.util.Map;


@Service("publisher")
public class PublisherImpl implements Publisher{
	@Autowired
	RabbitTemplate rabbitTemplate;

	public void sendfanoutMail(Mail mail) {
		rabbitTemplate.convertAndSend("fanout", "", mail);
	}

	public void senddirectMail(Mail mail, String routingkey) {
		rabbitTemplate.convertAndSend("direct", routingkey, mail);
	}

	public void sendtopicMail(Mail mail, String routingkey) {
		rabbitTemplate.convertAndSend("topic", routingkey, mail);
	}
	public void sendtDeadLetter(String orderExchange, String orderRoutingKey, Map<String, Object> map , String times) {
		rabbitTemplate.convertAndSend(orderExchange, orderRoutingKey, map, message -> {
			// 设置消息过期时间 10秒过期    如果过期时间内还没有被消费 就会发送给死信队列
			message.getMessageProperties().setExpiration(times);
			return message;
		});
	}
	/**
	 * 根据id，将消息顺序发送到对应的队列
	 * @param id  业务id
	 * @param msg 业务信息
	 */
	public void send(int id, String msg) {
		MessageInfo message = new MessageInfo(id, msg);
		rabbitTemplate.convertAndSend("direct_seq", String.valueOf(id % 2 + 1), message);
	}


	
}
